package com.zklh.leetcode.top100.problem128;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Nelson
 * @Description 解法1
 * @create 2023-02-07 15:29
 */
public class Solution1 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }

        int longestStreak = 0;

        for (Integer num : numSet) {
            if (!numSet.contains(num-1)) {
                int currentNum = num;
                int currentStreak = 1;
                while (numSet.contains(currentNum+1)) {
                    currentStreak += 1;
                    currentNum += 1;
                }
                longestStreak = Math.max(longestStreak, currentStreak);
            }

        }
        return longestStreak;
    }

    public static void main(String[] args) {
        Solution1 solution = new Solution1();
        System.out.println(solution.longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
        System.out.println(solution.longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));

    }
}
